package org.uim.cricket.analyzer.parsers;

class InputArgumentParser {

	private String[] args = null;
	private int argLen = 0;
	
	private boolean doAnalysis = false;
	private String analyzerExecutorType = null;
	private String analyzerReportType = "xml";
	
	private String scoreCardParserType = null;
	private String inputScoreCardFile = null;
	private String reportScoreCardFile = null;
	private String reportScoreCardType = "xml";
	
	private boolean doValidation = true;
	private String validationBeanID = "";
	
	public InputArgumentParser(String[] args) {
		this.args = args;
		this.argLen = args.length;
	}
	
	public void parseArguments() throws Exception {
		int curr = 0;
		
		if (args[curr].equalsIgnoreCase("-a")) {
			this.doAnalysis = true;
			curr++;
		}
		
		if (curr >= argLen) throw new Exception("Insufficient number of arguments!");
		
		if (args[curr].toLowerCase().startsWith("-type:")) {
			
			this.scoreCardParserType = args[curr].substring("-type:".length()).trim();
			curr++;
			if (curr >= argLen) throw new Exception("Insufficient number of arguments!"); 
			
			if (args[curr].toLowerCase().startsWith("-f")) {
				curr++;
				if (curr >= argLen) throw new Exception("Input/Output files can't be empty!");
				
				curr--;
				int pos = args[curr].indexOf(':');
				if (pos >= 0) {
					this.reportScoreCardType = args[curr].substring(pos+1);
				}
				curr++;
				
				this.inputScoreCardFile = args[curr];
				curr++;
				if (curr >= argLen) throw new Exception("Output file can't be empty!");
				this.reportScoreCardFile = args[curr];
				curr++;
				
			} else {
				
				throw new Exception("Unknown type of source/destination fetching for parser application! " +
							"This must indicate file type upto now by using \"-f\".");
			}
			
		} else {
			throw new Exception("argument #" + curr + " must be the type!");
		}
		
		if (!this.doAnalysis) return;
		
		if (curr >= argLen) throw new Exception("Analyzer executor type can't be empty!");
		if (args[curr].toLowerCase().startsWith("-aexec:")) {
			
			this.analyzerExecutorType = args[curr].substring("-aexec:".length());
			curr++;
			
		} else {
			throw new Exception("Unknown argument type for analyzer executor type! ["+args[curr]+"]");
		}
		
		if (curr >= argLen) return;
		if (args[curr].toLowerCase().startsWith("-arep:")) {
			
			this.analyzerReportType = args[curr].substring("-arep:".length());
			curr++;
			
		} else {
			throw new Exception("Unknown argument type for analyzer reporting type! ["+args[curr]+"]");
		}
		
		if (curr >= argLen) return;
		if (args[curr].startsWith("-v:")) {
			
			this.doValidation = true;
			this.validationBeanID = args[curr].substring("-v:".length());
			curr++;
			
		}  else if (args[curr].startsWith("-V")) {
			
			this.doValidation = false;
			this.validationBeanID = null;
			curr++;
			
		}
		
	}
	
	public void setArgs(String[] args) {
		this.args = args;
	}
	
	public String[] getArgs() {
		return args;
	}
	
	public void setDoAnalysis(boolean doAnalysis) {
		this.doAnalysis = doAnalysis;
	}
	
	public void setAnalyzerReportType(String analyzerReportType) {
		this.analyzerReportType = analyzerReportType;
	}
	
	public void setAnalyzerExecutorType(String analyzerExecutorType) {
		this.analyzerExecutorType = analyzerExecutorType;
	}
	
	public boolean isDoAnalysis() {
		return doAnalysis;
	}
	
	public String getAnalyzerReportType() {
		return analyzerReportType;
	}
	
	public String getAnalyzerExecutorType() {
		return analyzerExecutorType;
	}
	
	public void setScoreCardParserType(String scoreCardParserType) {
		this.scoreCardParserType = scoreCardParserType;
	}
	
	public String getScoreCardParserType() {
		return scoreCardParserType;
	}
	
	public void setReportScoreCardFile(String reportScoreCardFile) {
		this.reportScoreCardFile = reportScoreCardFile;
	}
	
	public void setInputScoreCardFile(String inputScoreCardFile) {
		this.inputScoreCardFile = inputScoreCardFile;
	}
	
	public String getReportScoreCardFile() {
		return reportScoreCardFile;
	}
	
	public String getInputScoreCardFile() {
		return inputScoreCardFile;
	}
	
	public void setReportScoreCardType(String reportScoreCardType) {
		this.reportScoreCardType = reportScoreCardType;
	}
	
	public String getReportScoreCardType() {
		return reportScoreCardType;
	}
	
	public void setDoValidation(boolean doValidation) {
		this.doValidation = doValidation;
	}
	
	public boolean isDoValidation() {
		return doValidation;
	}
	
	public void setValidationBeanID(String validationBeanID) {
		this.validationBeanID = validationBeanID;
	}
	
	public String getValidationBeanID() {
		return validationBeanID;
	}
	
}
